package cn.hx.mall.sell.controller;

import cn.hx.mall.sell.entity.SmsHomeBrand;
import cn.hx.mall.sell.service.SmsHomeBrandService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 首页推荐品牌表(SmsHomeBrand)表控制层
 *
 * @author makejava
 * @since 2022-11-02 16:10:31
 */
@Controller
@RequestMapping("sell")
public class SmsHomeBrandController {
    /**
     * 服务对象
     */
    @Resource
    private SmsHomeBrandService smsHomeBrandService;

    @GetMapping("/brand")
    public String brandList(ModelMap map, SmsHomeBrand smsHomeBrand,
                                     @RequestParam(value = "page",defaultValue = "1") Integer page,
                                     @RequestParam(value = "size", defaultValue = "5") Integer size) {
        Page<SmsHomeBrand> smsHomeBrands = PageHelper.startPage(page, size);
        smsHomeBrandService.queryAll(smsHomeBrand);
        PageInfo<SmsHomeBrand> pageInfo = new PageInfo<>(smsHomeBrands);
        map.addAttribute("brands", pageInfo);
        return "sell/brand";
    }

    @GetMapping("deleteByBrandId/{id}")
    @ResponseBody
    public Map<String, Object> delete(@PathVariable Integer id) {
        boolean b = smsHomeBrandService.deleteById(id);
        HashMap<String, Object> map = new HashMap<>();
        if(b) {
            map.put("code", 1);
        }
        return map;
    }

    @PostMapping("selectBrand")
    public String likeAll(SmsHomeBrand smsHomeBrand, ModelMap map, @RequestParam(value = "page",defaultValue = "1") Integer page,
                          @RequestParam(value = "size", defaultValue = "5") Integer size) {
        Page<SmsHomeBrand> smsHomeBrands = PageHelper.startPage(page, size);
        smsHomeBrandService.queryAll(smsHomeBrand);
        PageInfo<SmsHomeBrand> pageInfo = new PageInfo<>(smsHomeBrands);
        map.addAttribute("brands", pageInfo);
        return "sell/brand";
    }

    @PostMapping("/updateBrandSort")
    public String save(SmsHomeBrand smsHomeBrand,String path) {
        smsHomeBrandService.update(smsHomeBrand);
        return "redirect:/"+path;
    }

    @GetMapping("/selectByBrandId/{id}")
    @ResponseBody
    public Map<String, Object> queryById(@PathVariable Integer id) {
        SmsHomeBrand smsHomeBrand = smsHomeBrandService.queryById(id);
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("data", smsHomeBrand);
        return resMap;
    }
    @GetMapping("/addBrand")
    public String add(){
        return "sell/addBrand";
    }
    @PostMapping("/addBrand")
    public String brand(ModelMap map, SmsHomeBrand smsHomeBrand){
        smsHomeBrandService.insert(smsHomeBrand);
        map.addAttribute("brands",smsHomeBrandService.queryAllByLimit(0,1000));
        return "redirect:/sell/brand";
    }
    /*状态按钮改变*/
    @GetMapping("statusByBrandId/{id}")
    @ResponseBody
    public Map<String, Object> statusById(@PathVariable Integer id) {
        SmsHomeBrand smsHomeBrand = smsHomeBrandService.queryById(id);
        Integer status = smsHomeBrand.getRecommendStatus();
        if (status.equals(1)) {
            smsHomeBrand.setRecommendStatus(0);
        } else {
            smsHomeBrand.setRecommendStatus(1);
        }
        HashMap<String,Object> map = new HashMap<>();
        SmsHomeBrand update = smsHomeBrandService.update(smsHomeBrand);
        if(update!=null) {
            map.put("code", 1);
        }
        return map;
    }
}